package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysCurrency;
import cn.wizzer.app.sys.modules.services.SysCurrencyService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;


@IocBean
@At("/platform/sys/currency")
public class SysCurrencyController {
    private static final Log log = Logs.get();
    @Inject
    @Reference
    private SysCurrencyService sysCurrencyService;

    @At("")
    @Ok("beetl:/platform/sys/currency/index.html")
    @RequiresPermissions("sys.finance.currency.list")
    public void index() {
    }

    @At
    @Ok("json")
    public Object list(String name) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(name)) {
            cnd.and("name", "like", "%" + name + "%");
        }
        return Result.success().addData(sysCurrencyService.query(cnd));
    }

    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.currency.delete")
    @SLog(tag = "删除币种", msg = "删除币种:${id}")
    public Object delete(String id) {
        sysCurrencyService.vDelete(id);
        return Result.success();
    }

    @At
    @Ok("json:full")
    @RequiresPermissions("sys.finance.currency.list")
    public Object data(@Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize, String searchKeyword) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchKeyword)) {
            cnd.and("name", "like", "%" + searchKeyword + "%");
        }
        cnd.and("delFlag", "=", 0);
        return Result.success(sysCurrencyService.listPage(pageNumber, pageSize, cnd));
    }

    @At("/edit/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.currency.list")
    public Object edit(String id) {
        return Result.success().addData(sysCurrencyService.fetch(id));
    }


    @At
    @Ok("json")
    @RequiresPermissions("sys.finance.currency.edit")
    @SLog(tag = "修改参数", msg = "修改币种${args[0].id}")
    public Object editDo(@Param("..") SysCurrency currency) {
        currency.setOpBy(StringUtil.getPlatformUid());
        currency.setOpAt(1L);
        sysCurrencyService.update(currency);
        return Result.success();
    }

    @At
    @Ok("json")
    @RequiresPermissions("sys.finance.currency.add")
    @SLog(tag = "添加币种", msg = "添加币种${args[0].name}")
    public Object add(@Param("..") SysCurrency currency) {
        currency.setOpBy(StringUtil.getPlatformUid());
        currency.setOpAt(1L);
        currency.setId(currency.getName().toLowerCase());
        sysCurrencyService.insert(currency);
        return Result.success();
    }


    /**
     * 启用币种
     *
     * @param id
     * @return
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.currency.edit")
    @SLog(tag = "启用币种", msg = "币种id:${id}")
    public Object enable(String id) {
        sysCurrencyService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        return Result.success();
    }

    /**
     * 禁用币种
     *
     * @param id
     * @return
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.currency.edit")
    @SLog(tag = "禁用币种", msg = "币种id:${id}")
    public Object disable(String id) {
        sysCurrencyService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        return Result.success();
    }
}
